package subscriberPanels;

import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

import clientGUI.PanelDesign;
import clientGUI.MainGUI;
import clientGUI.ConnectionGUI;
import javax.swing.JPanel;
import java.awt.Font;

public class UpdateDetailsPanel extends PanelDesign {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textField;
	public static JTextField phoneNumtextField;
	public static JTextField eMailtextField;
	public JPasswordField oldPwtextField;
	public JPasswordField newPwtextField;
	private Pattern pattern2;
	private Matcher matcher;
	public JButton btnSubmit;
	public JButton btnChangePassword;
	private static final String PASSWORD_PATTERN= "(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{6,20}";
	public static JTextField firstNametextField;
	public static JTextField lastNametextField;
	/**
	 * Create the panel.
	 */
	public UpdateDetailsPanel() {
		setBorder(new LineBorder(new Color(0, 0, 0), 3, true));
		setPreferredSize(new Dimension(644, 462));
		pattern2 = Pattern.compile(PASSWORD_PATTERN);
		setBorder(new LineBorder(new Color(0, 0, 0), 3,true));
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(35, 93, 268, 170);
		add(panel);
		panel.setLayout(null);
		phoneNumtextField = new JTextField();
		phoneNumtextField.setBounds(107, 0, 161, 20);
		panel.add(phoneNumtextField);
		phoneNumtextField.setColumns(10);
		

		
		phoneNumtextField.setText(ConnectionGUI.user.phone);
		
		eMailtextField = new JTextField();
		eMailtextField.setBounds(107, 32, 161, 20);
		panel.add(eMailtextField);
		eMailtextField.setColumns(10);
		eMailtextField.setText(ConnectionGUI.user.email);
		
		firstNametextField = new JTextField();
		firstNametextField.setBounds(107, 64, 161, 20);
		panel.add(firstNametextField);
		firstNametextField.setColumns(10);
		firstNametextField.setText(ConnectionGUI.user.firstName);
		
		lastNametextField = new JTextField();
		lastNametextField.setBounds(107, 96, 161, 20);
		panel.add(lastNametextField);
		lastNametextField.setColumns(10);
		lastNametextField.setText(ConnectionGUI.user.lastName);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.setBounds(58, 139, 142, 31);
		panel.add(btnSubmit);
		
		JLabel lblPhoneNumber = new JLabel("Phone number:");
		lblPhoneNumber.setBounds(0, 3, 89, 14);
		panel.add(lblPhoneNumber);
		
		JLabel lblEmail = new JLabel("E-Mail:");
		lblEmail.setBounds(0, 35, 87, 14);
		panel.add(lblEmail);
		
		JLabel lblFirstName = new JLabel("First name:");
		lblFirstName.setBounds(0, 67, 81, 14);
		panel.add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last name:");
		lblLastName.setBounds(0, 99, 81, 14);
		panel.add(lblLastName);
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				String[] s=new String[6];	
				s[0]="UpdateDetails";
				s[1]=phoneNumtextField.getText();
				s[2]=eMailtextField.getText();
				s[3]=ConnectionGUI.user.id;
				s[4]=firstNametextField.getText();
				s[5]=lastNametextField.getText();
				boolean x1=!MainGUI.isNumber(s[1])||(s[1].length()!=10),x2=!MainGUI.validateEmail(s[2]);
				if((s[1].equals(""))||(s[2].equals(""))||(s[4].equals(""))||(s[5].equals("")))
				{
					if(!MainGUI.testmode)
					
						JOptionPane.showMessageDialog(null,"You must fill out all forms!");
					return;
				}
				else if(x1&&x2)
				{
					if(!MainGUI.testmode)
						JOptionPane.showMessageDialog(null,"Invalid phone number and email address!");
					return;
				}
				else if(x1)
				{
					if(!MainGUI.testmode)
						JOptionPane.showMessageDialog(null,"Invalid phone number!");
					return;
				}
				else if(x2)
				{
					if(!MainGUI.testmode)
						JOptionPane.showMessageDialog(null,"Invalid email address!");
					return;
				}
				else if(!MainGUI.isName(s[4])||!MainGUI.isName(s[5]))
				{
					if(!MainGUI.testmode)
						JOptionPane.showMessageDialog(null,"First name and last name must include only letters!");
					return;
				}
				if(!MainGUI.testmode)
				{
				int n = JOptionPane.showConfirmDialog(new JFrame(),	"Are you sure you want to update your details?","An Inane Question",JOptionPane.YES_NO_OPTION);		
				if(n==1)		
					return;	
				}
				ConnectionGUI.user.email=s[2];
				ConnectionGUI.user.phone=s[1];
				ConnectionGUI.user.firstName=s[4];
				ConnectionGUI.user.lastName=s[5];
				MainGUI.OBLClient.handleMessageFromClient(s);
			}
		});
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(351, 93, 270, 106);
		add(panel_1);
		panel_1.setLayout(null);
		
		oldPwtextField = new JPasswordField();
		oldPwtextField.setBounds(109, 0, 161, 20);
		panel_1.add(oldPwtextField);
		oldPwtextField.setColumns(10);
		
		newPwtextField = new JPasswordField();
		newPwtextField.setBounds(109, 32, 161, 20);
		panel_1.add(newPwtextField);
		newPwtextField.setColumns(10);
		
		JLabel lblOldPassword = new JLabel("Old password:");
		lblOldPassword.setBounds(0, 3, 98, 14);
		panel_1.add(lblOldPassword);
		
		JLabel lblNewPassword = new JLabel("New password:");
		lblNewPassword.setBounds(0, 35, 98, 14);
		panel_1.add(lblNewPassword);
		
		btnChangePassword = new JButton("Change password");
		btnChangePassword.setBounds(59, 75, 142, 31);
		panel_1.add(btnChangePassword);
		btnChangePassword.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				String[] s=new String[5];	
				s[0]="UpdateDetails";
				s[1]="ChangePassword";
				s[2]=oldPwtextField.getText();
				s[3]=newPwtextField.getText();
				s[4]=MainGUI.OBLClient.ClientID;
				
				if(!validatePassword(s[3]))
				{
					if(!MainGUI.testmode)
					{
						JOptionPane.showMessageDialog(null,"ERROR: New password does not mach necessary pattern!");
					}
						return;
					
				}
				if(!MainGUI.testmode)
				{
					int n = JOptionPane.showConfirmDialog(new JFrame(),	"Are you sure you want to change your password?","An Inane Question",JOptionPane.YES_NO_OPTION);		
					if(n==1)		
						return;
				}
				MainGUI.OBLClient.handleMessageFromClient(s);
			}
		});

		
		JLabel lblUpdateDetails = new JLabel("Update Details");
		lblUpdateDetails.setFont(new Font("Tahoma", Font.PLAIN, 29));
		lblUpdateDetails.setBounds(212, 11, 220, 49);
		add(lblUpdateDetails);
		
		JLabel lblEnterNewDetails = new JLabel("Enter new details:");
		lblEnterNewDetails.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblEnterNewDetails.setBounds(88, 60, 180, 33);
		add(lblEnterNewDetails);
		
		JLabel lblChangePassword = new JLabel("Change password:");
		lblChangePassword.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblChangePassword.setBounds(404, 60, 180, 33);
		add(lblChangePassword);
		remove(background);
		add(background);
	}
	


	/** uses regex class to check weather a password is legal.
	 * @param pw - password we want to check
	 * @return "true" if password is legal.
	 * 			"false" otherwise.
	 */
	public boolean validatePassword(final String pw) { 
		matcher = pattern2.matcher(pw);
		return matcher.matches();
 
	}
	
	/** change the content in the panels text fields
	 * @param x - information to be added to text fields
	 */
	public static void changeTextFieldContent(String[][] x)
	{
		phoneNumtextField.setText(x[0][0]);
		eMailtextField.setText(x[0][1]);
		firstNametextField.setText(x[0][2]);
		lastNametextField.setText(x[0][3]);
	}
	
	/** opens an option pane with the message from the server 
	 * @param info - message from server
	 */
	public static void ShowMsgFromServer(String info)
	{

		if(info.compareTo("1")==0)
		{
			info=new String("Information updated successfully!");
		}
		
		JOptionPane.showMessageDialog(null,info);
	}
}
